package com.example.doubledatasource.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.doubledatasource.entity.User;
import com.example.doubledatasource.mapper.UserMapper;
import com.example.doubledatasource.query.UserQuery;
import com.example.doubledatasource.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户服务实现类 - 使用主数据源
 *
 * @author example
 * @since 1.0.0
 */
@Service
@DS("master") // 指定使用主数据源
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User findByUsername(String username) {
        return baseMapper.findByUsername(username);
    }

    @Override
    public List<User> findActiveUsers() {
        return baseMapper.findActiveUsers();
    }

    @Override
    public IPage<User> findUsersByQuery(UserQuery query) {
        // 验证并设置默认值
        query.validate();

        // 创建分页对象
        Page<User> page = new Page<>(query.getPage(), query.getSize());

        // 使用自定义的分页查询方法
        return baseMapper.selectUsersByQuery(page, query);
    }
}
